package com.ruoyi.customer.controller;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.customer.domain.CustomerBind;
import com.ruoyi.customer.domain.CustomerBindVO;
import com.ruoyi.customer.domain.WxCustomerBind;
import com.ruoyi.customer.service.ICustomerBindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 家属绑定Controller
 * 
 * @author luogongquan
 * @date 2024-09-11
 */
@RestController
@RequestMapping("/customerBind")
public class CustomerBindController extends BaseController
{
    @Autowired
    private ICustomerBindService customerBindService;

    /**
     * 查询家属绑定列表
     */
    @RequiresPermissions("customer:customerBind:list")
    @GetMapping("/list")
    public TableDataInfo list(CustomerBind customerBind)
    {
        startPage();
        List<CustomerBind> list = customerBindService.selectCustomerBindList(customerBind);
        return getDataTable(list);
    }

    /**
     * 查询家属绑定列表
     */
    @GetMapping("wx/bindList")
    public TableDataInfo wxBindList(CustomerBindVO customerBind)
    {
        startPage();
        customerBind.setCustomerId(SecurityUtils.getUserId());
        List<CustomerBindVO> list = customerBindService.selectCustomerBindList2(customerBind);
        return getDataTable(list);
    }

    /**
     * 查询被家属绑定的用户列表
     */
    @GetMapping("wx/wardList")
    public AjaxResult wardList()
    {
        List<CustomerBindVO> list = customerBindService.selectCustomerWardList();
        return success(list);
    }
    /**
     * 查询家属绑定列表
     */
    @RequiresPermissions("customer:customerBind:list")
    @GetMapping("/list2")
    public TableDataInfo list2(CustomerBindVO customerBind)
    {
        startPage();
        List<CustomerBindVO> list = customerBindService.selectCustomerBindList2(customerBind);
        return getDataTable(list);
    }

    /**
     * 导出家属绑定列表
     */
    @RequiresPermissions("customer:customerBind:export")
    @Log(title = "家属绑定", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomerBind customerBind)
    {
        List<CustomerBind> list = customerBindService.selectCustomerBindList(customerBind);
        ExcelUtil<CustomerBind> util = new ExcelUtil<CustomerBind>(CustomerBind.class);
        util.exportExcel(response, list, "家属绑定数据");
    }

    /**
     * 获取家属绑定详细信息
     */
    @RequiresPermissions("customer:customerBind:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(customerBindService.selectCustomerBindById(id));
    }

    /**
     * 新增家属绑定
     */
    @RequiresPermissions("customer:customerBind:add")
    @Log(title = "家属绑定", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerBind customerBind)
    {
        return toAjax(customerBindService.insertCustomerBind(customerBind));
    }

    /**
     * 新增家属绑定
     */
    @Log(title = "微信端家属绑定", businessType = BusinessType.INSERT)
    @PostMapping("wx/add")
    public AjaxResult wxAdd(@RequestBody WxCustomerBind customerBind)
    {
        clearPage();
        if(customerBind.getPhoneNumber()==null){
            throw new ServiceException("手机号码不能为空");
        }
        customerBind.setCustomerId(SecurityUtils.getUserId());
        return toAjax(customerBindService.insertCustomerBind4Wx(customerBind));
    }
    /**
     * 新增家属绑定
     */
    @Log(title = "微信端家属解绑", businessType = BusinessType.INSERT)
    @GetMapping("wx/delete/{id}")
    public AjaxResult wxDelete(@PathVariable Long id)
    {
        if(id==null){
            throw new ServiceException("解绑对象不能为空");
        }
        return toAjax(customerBindService.deleteCustomerBindByIds(new Long[]{id}));
    }

    /**
     * 修改家属绑定
     */
    @RequiresPermissions("customer:customerBind:edit")
    @Log(title = "家属绑定", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerBind customerBind)
    {
        return toAjax(customerBindService.updateCustomerBind(customerBind));
    }

    /**
     * 删除家属绑定
     */
    @RequiresPermissions("customer:customerBind:remove")
    @Log(title = "家属绑定", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(customerBindService.deleteCustomerBindByIds(ids));
    }
}
